<?
namespace Admin\Model
{
    /**
     * Klasa dodająca obsługę filmów z serwisu youtube
     *
     */
    class Film extends \Api\Model\DB2
    {            
        static public function parseLink($link)
        {
            // // www.youtube.com/watch?v=YxyDbaQSWqI            
            $qsyntax = strpos($link,'?');            
            
            $link = substr($link, $qsyntax+1);            
            $tags = explode('&',$link);
            $vid_id = false;
            
            foreach($tags as $k => $v)
            {
                $t=explode('=',$v);
                
                if($t[0]=='v'){
                  $vid_id=$t[1];                  
                  continue;
                }
            }            
            return $vid_id;
        }
        public function add($link, $name)
        {
            $Q = $this->db->prepare('INSERT INTO arch_film VALUES(0,:name,"",:link)');
            $Q->bindValue(':name',$name,\PDO::PARAM_STR);
            $Q->bindValue(':link',$link,\PDO::PARAM_STR);
            
            if($Q->execute())
            {
                return true;
            }
            
            return false;
        }
        
        public function get()
        {
            $Q = $this->db->query('SELECT *, md5(CONCAT(md5(`id`),md5(`link`))) as hash FROM `arch_film`');
            return $Q->fetchAll();
        }
        
        public function erase($hash)
        {
            $Q = $this->db->prepare('DELETE FROM `arch_film` WHERE md5(CONCAT(md5(`id`),md5(`link`))) = :hash');
            $Q->bindValue(':hash',$hash);
            
            if($Q->execute()) return true;
            return false;
        }
    }
}
?>